package com.sinosoft.lk.lkssosampleclient.config;

import com.sinosoft.lk.core.client.SsoClientFilter;
import com.sinosoft.lk.core.client.SsoLoginSuccessCallBack;
import com.sinosoft.lk.core.config.SsoClientFilterConfig;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 绫小路
 * @date 2021/3/6 23:48
 * @description
 */
@Configuration
public class SsoConfig {

  @Bean
  public FilterRegistrationBean ssoClientFilterConfig() {
    FilterRegistrationBean bean = new FilterRegistrationBean();

    SsoClientFilterConfig config = new SsoClientFilterConfig();
    config.setLoginPath("http://localhost:8888/sso_login");
    Set<String> ex = new HashSet<>();
    ex.add("/ex");
    ex.add("/sso_login");
    config.setExcludedPaths(ex);

    // set redis info
    config.setJedisAddress("redis://127.0.0.1:6379");
    config.setJedisConnectionTimeout(10000);
    // set pool
    //config.setJedisPoolConfig();

    //my login success callback
    config.setSsoLoginSuccessCallBack(new SsoLoginSuucessCallback());

    SsoClientFilter ssoClientFilter = new SsoClientFilter();
    ssoClientFilter.setSsoClientFilterConfig(config);
    bean.setFilter(ssoClientFilter);
    bean.addUrlPatterns("/*");
    bean.setName("client");
    bean.setOrder(-666);
    return bean;
  }

}

class SsoLoginSuucessCallback implements SsoLoginSuccessCallBack {

  @Override
  public void success(HttpServletRequest request, HttpServletResponse response, Map<String, String> userMap) {
    System.out.println("sso登录成功回调函数----------------------------------");
    System.out.println(userMap);
  }
}
